package gupao.springboot.redis.config;

import lombok.RequiredArgsConstructor;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.ArrayList;
import java.util.List;

@Configuration
@RequiredArgsConstructor
@EnableCaching
public class RedisConfig {

    private final MessageListener messageListener;

    @Bean
    RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }


    @Bean
    RedisMessageListenerContainer redisContainer(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        List<Topic> list = new ArrayList<>();
        list.add(new PatternTopic("hui*"));
        list.add(new PatternTopic("test"));
        list.add(new PatternTopic("TOU"));
        container.addMessageListener(messageListener, list);
        return container;
    }

  /*  @Bean(name = "redissonClient")
    RedissonClient getRedissonClient() {
        Config config = new Config();
        config.useClusterServers()        //cluster方式至少6个节点(3主3从，3主做sharding，3从用来保证主宕机后可以高可用)
                .addNodeAddress("redis://192.168.8.127:6381")
                .addNodeAddress("redis://192.168.8.128:6381")
                .addNodeAddress("redis://192.168.8.129:6381")
                .addNodeAddress("redis://192.168.8.127:6380")
                .addNodeAddress("redis://192.168.8.128:6380")
                .addNodeAddress("redis://192.168.8.129:6380");
        return Redisson.create(config);
    }*/


    @Bean(name="redissonClient1")
    RedissonClient getRedissonClient1() {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://192.168.139.110:6379");
        RedissonClient redissonClient1= Redisson.create(config);
        return redissonClient1;
    }

}
